package com.spring.controller.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.ListUtils;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.spring.domain.User;
import com.spring.mail.MailService;
import com.spring.service.user.IUser;
import com.utils.Validation;

public class LoginController extends MultiActionController {
	private static final Logger logger = Logger.getLogger(LoginController.class);
    private IUser userService;
    
    private static final String LOGIN_VIEW ="login/login";
    private static final String ADMIN_LOGGED_VIEW ="index";
    
    public ModelAndView logOut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    	List<String> 	messages	= new ArrayList<String>();
    	User 			user 		= request.getSession().getAttribute("admin") != null ? (User)request.getSession().getAttribute("admin") : null;
    	
    	if(user!=null){
    		logger.info("LogOut for Admin:["+user+"]");
    		request.getSession().removeAttribute("admin");
    		messages.add("login.okLogout");
    	}
       
        Map<String,Object> model = new HashMap<String, Object>();
        model.put("messages",messages);	
        return new ModelAndView(LOGIN_VIEW, "model", model);
    }
    
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    	List<String> 	messages	= new ArrayList<String>();
    	String 			userName	= ServletRequestUtils.getStringParameter(request, "mail", null);
    	String 			pwd			= ServletRequestUtils.getStringParameter(request, "pwd", null);
    	String 			view		= LOGIN_VIEW;
    	
    	if(userName!=null && pwd!=null){
    		logger.info("Trying get admin for userName:["+userName+"] and pwd:["+pwd+"]");
    		User user = this.userService.login(userName,pwd);
    		if(user!=null && user.getAdmin()==1){
    			logger.info("FOUND ADMIN USER:["+user.toString()+"]");
    			request.getSession().setAttribute("admin", user);
    			view=ADMIN_LOGGED_VIEW;
    		}else{
    			messages.add("login.incorrect");
    		}
    	}else{
    		messages.add("login.incorrect");
    	}
       
        Map<String,Object> model = new HashMap<String, Object>();
        model.put("messages",messages);
        logger.info("Returning "+view+" view");
        return new ModelAndView(view, "model", model);
    }

    /**
     * 	REDIREZIONI A JSP
     * */
    public ModelAndView loginPage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    	logger.info("Returning "+LOGIN_VIEW+" view");
        return new ModelAndView(LOGIN_VIEW);
    }
    
    public ModelAndView adminLoggedPage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    	logger.info("Returning "+ADMIN_LOGGED_VIEW+" view");
        return new ModelAndView(ADMIN_LOGGED_VIEW);
    }
    
    
    /**
     * SETTER E GETTER
     * */
	public IUser getUserService() {
		return userService;
	}

	public void setUserService(IUser userService) {
		this.userService = userService;
	}

}
